package signGrammar.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.border.Border;

public class CViewBox extends JPanel{
	private static final long serialVersionUID = 3L;
	
	public enum BoxType{
		HORIZONTAL,
		VERTICAL;
	}

	protected final BoxType boxType;
	protected GridBagConstraints constrs = new GridBagConstraints();
	public CViewBox(BoxType boxType, Container... things) {
		this.boxType = boxType;
		init();
		for( Container thing : things )
			add(thing, updateConstraints(constrs) );
	}
	
	private void init(){
		setLayout(new GridBagLayout());
		constrs.insets = new Insets(1,1,1,1);
		constrs.gridx = constrs.gridy = -1;
		constrs.gridwidth = 1;
		constrs.gridheight= 1;
		constrs.weightx = 1;
		constrs.weighty = 1;			
		constrs.fill = GridBagConstraints.BOTH;
	}

	@Override
	public CViewBox add(Component c){
		add( c, updateConstraints(constrs) );
		return this;
	}
	
	public CViewBox setBoxBorder( Border border ){
		setBorder( border );
		return this;
	}
	
	@Override
	public void removeAll(){
		super.removeAll();
		init();
	}
	
	protected GridBagConstraints updateConstraints(GridBagConstraints constrs) {
		switch ( boxType ) {
		case HORIZONTAL:
			constrs.gridx++;
			break;
		case VERTICAL:
			constrs.gridy++;
		default:
			break;
		}
		return constrs;
	}
}